package com.nwcl.ysx.controller;

import com.nwcl.basic.bo.ResourcesBO;
import com.nwcl.basic.service.ResourcesService;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.result.Result;
import com.nwcl.common.utils.BeanUtil;
import com.nwcl.ysx.bo.*;
import com.nwcl.ysx.service.*;
import com.nwcl.ysx.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/*
 * @Description: 餐厅功能接口
 * @Author liguiqin
 * @Date 2023/5/25 16:28
 * @param: null
 * @return
 * @UpdateDate 2023/5/25 16:28
 **/
@RestController
@RequestMapping("/api/ysx/restaurant")
public class ApiRestaurantController {
    @Autowired
    private SpaceService spaceService;
    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private ResourcesService resourcesService;
    @Autowired
    private RestaurantDishesService restaurantDishesService;
    @Autowired
    private RestaurantRoomService restaurantRoomService;
    @Autowired
    private CuisineService cuisineService;
    @Autowired
    private CuisineRoomService cuisineRoomService;

    /*
     * @Description:   首页信息
     * @Author liguiqin
     * @Date 2023/5/25 17:35
     * @param:
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/25 17:35
     **/
    @GetMapping("/index")
    public Result index() {
        IndexVO vo = new IndexVO();
        Integer spaceId = 1;//默认空间id
        IndexRestaurantVO restaurantVO = new IndexRestaurantVO();//餐厅信息
        IndexDishesVO dishesVO = new IndexDishesVO();//菜品信息
        List<IndexRoomVO> roomVOList = new ArrayList<>();//房间列表
        SpaceBO spaceBO = spaceService.getById(spaceId);//获取默认id为1的空间信息
        if (spaceBO == null) {
            return Result.success(vo);
        }
        vo.setId(spaceId);
        vo.setAddress(spaceBO.getAddress());
        vo.setLatitude(spaceBO.getLatitude());
        vo.setLongitude(spaceBO.getLongitude());
        vo.setDescribe(spaceBO.getDescribe());
        vo.setTel(spaceBO.getTel());
        //设置餐厅信息,餐厅默认图
        RestaurantBO restaurantBO = restaurantService.getBySpaceId(spaceBO.getId());
        if(restaurantBO != null){
            restaurantVO.setId(restaurantBO.getId());
            restaurantVO.setDescribe(restaurantBO.getDescribe());
            restaurantVO.setName(restaurantBO.getName());
            restaurantVO.setDefaultImg(restaurantBO.getDefaultImg());
        }
        //设置菜品信息,菜品默认图
        initDishes(dishesVO, restaurantBO);
        //设置包房信息(取前三个)
        initRoomList(roomVOList, restaurantBO);
        setCuisine(restaurantBO, roomVOList);
        vo.setRestaurant(restaurantVO);
        vo.setDishes(dishesVO);
        vo.setRoomList(roomVOList);
        //设置环境信息
        List<ResourcesBO> resourcesBOS= resourcesService.listByPidTypeAndPids(Constant.ResourcesPidType.space, Arrays.asList(spaceId));
        List<ResourceVO> resourceVOS=BeanUtil.copyList(resourcesBOS, ResourceVO.class);
        vo.setEnvList(resourceVOS);
        return Result.success(vo);
    }


    /*
     * @Description: 餐厅详情
     * @Author liguiqin
     * @Date 2023/5/29 15:10
     * @param: id
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/29 15:10
     **/
    @GetMapping("/details")
    public Result details(Integer id) {
        if (id == null) {
            return Result.fail("餐厅id不能为空");
        }
        RestaurantBO restaurantBO = restaurantService.getById(id);
        RestaurantDetailsVO detailsVO = BeanUtil.copy(restaurantBO, RestaurantDetailsVO.class);
        //设置包房图片资源
        List<ResourcesBO> resourcesBOS = resourcesService.listByPidTypeAndPids(Constant.ResourcesPidType.restaurant, Arrays.asList(id));
        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourcesBOS, ResourceVO.class);
        detailsVO.setResourceList(resourceVOS);
        return Result.success(detailsVO);
    }

    /*
     * @Description: 查询包房列表
     * @Author liguiqin
     * @Date 2023/5/30 13:50
     * @param: restaurantId
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/30 13:50
     **/
    @GetMapping("/roomList")
    public Result roomList(Integer restaurantId) {
        if (restaurantId == null) {
            return Result.fail("餐厅id不能为空");
        }
        List<RestaurantRoomBO> roomBOS = restaurantRoomService.listByRestaurantId(restaurantId);
        List<IndexRoomVO> roomVOS = BeanUtil.copyList(roomBOS, IndexRoomVO.class);
        List<Integer> roomIds = new ArrayList<>();
        for (IndexRoomVO roomVO : roomVOS) {
            roomVO.setPeople(roomVO.getPeopleMin() + "~" + roomVO.getPeopleMax() + "人");
            roomIds.add(roomVO.getId());
        }
        RestaurantBO restaurantBO = restaurantService.getById(restaurantId);
        setCuisine(restaurantBO, roomVOS);
        return Result.success(roomVOS);
    }


    /*
     * @Description: 包房详情
     * @Author liguiqin
     * @Date 2023/5/29 15:10
     * @param: id
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/29 15:10
     **/
    @GetMapping("/roomDetails")
    public Result roomDetails(Integer id) {
        if (id == null) {
            return Result.fail("包房id不能为空");
        }
        RestaurantRoomBO roomBO = restaurantRoomService.getById(id);
        RoomDetailsVO detailsVO = BeanUtil.copy(roomBO, RoomDetailsVO.class);
        detailsVO.setPeople(roomBO.getPeopleMin() + "~" + roomBO.getPeopleMax() + "人");
        //设置包房图片资源
        List<ResourcesBO> resourcesBOS = resourcesService.listByPidTypeAndPids(Constant.ResourcesPidType.room, Arrays.asList(id));
        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourcesBOS, ResourceVO.class);
        detailsVO.setResourceList(resourceVOS);
        //设置菜系
        Map<Integer, CuisineBO> cuisineMap = new HashMap<>();
        List<CuisineBO> cuisineList = cuisineService.listByRestaurantId(roomBO.getRestaurantId());
        if (CollectionUtils.isEmpty(cuisineList)) {
            return Result.success(detailsVO);
        }
        cuisineMap = cuisineList.stream().collect(Collectors.toMap(CuisineBO::getId, Function.identity()));
        List<Integer> cuisineIds = cuisineList.stream().map(CuisineBO::getId).collect(Collectors.toList());
        CuisineRoomBO cuisineRoomBO = cuisineRoomService.listCuisineByRoomId(roomBO.getId(), cuisineIds);
        if (cuisineRoomBO != null) {
            CuisineBO cuisineBO = cuisineMap.get(cuisineRoomBO.getCuisineId());
            detailsVO.setCuisine(BeanUtil.copy(cuisineBO, CuisineVO.class));
        }
        return Result.success(detailsVO);
    }

    /*
     * @Description: 菜品列表
     * @Author liguiqin
     * @Date 2023/5/29 16:18
     * @param: restaurantId
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/29 16:18
     **/
    @GetMapping("dishesList")
    public Result dishesList(Integer restaurantId) {
        if (restaurantId == null) {
            return Result.fail("餐厅id不能为空");
        }
        List<RestaurantDishesBO> dishesBOS = restaurantDishesService.listByRestaurantId(restaurantId);
        List<RestaurantDishesVO> dishesVOS = BeanUtil.copyList(dishesBOS, RestaurantDishesVO.class);
        return Result.success(dishesVOS);
    }

    /*
     * @Description:设置菜系信息
     * @Author liguiqin
     * @Date 2023/5/31 10:22
     * @param: dishesVO
     * @param: restaurantBO
     * @return void
     * @UpdateDate 2023/5/31 10:22
     **/
    private void initDishes(IndexDishesVO dishesVO, RestaurantBO restaurantBO) {
        if (restaurantBO == null) {
            return;
        }
        List<RestaurantDishesBO> dishesBOS = restaurantDishesService.listByRestaurantId(restaurantBO.getId());
        if (!CollectionUtils.isEmpty(dishesBOS)) {
            RestaurantDishesBO dishesBO = dishesBOS.get(0);
            dishesVO.setId(dishesBO.getId());
            dishesVO.setDescribe(dishesBO.getDescribe());
            dishesVO.setName(dishesBO.getName());
            dishesVO.setDefaultImg(dishesBO.getDefaultImg());
        }
    }

    /*
     * @Description:  设置包房信息
     * @Author liguiqin
     * @Date 2023/5/31 10:22
     * @param: roomVOList
     * @param: restaurantBO
     * @return void
     * @UpdateDate 2023/5/31 10:22
     **/
    private void initRoomList(List<IndexRoomVO> roomVOList, RestaurantBO restaurantBO) {
        if (restaurantBO == null) {
            return;
        }
        List<RestaurantRoomBO> roomBOList = restaurantRoomService.listByRestaurantId(restaurantBO.getId());
        if (roomBOList.size() > 3) {
            roomBOList = roomBOList.subList(0, 3);
        }
        for (RestaurantRoomBO roomBO : roomBOList) {
            IndexRoomVO roomVO = new IndexRoomVO();
            roomVO.setId(roomBO.getId());
            roomVO.setName(roomBO.getName());
            roomVO.setDefaultImg(roomBO.getDefaultImg());
            roomVO.setPeopleMax(roomBO.getPeopleMax());
            roomVO.setPeopleMin(roomBO.getPeopleMin());
            roomVO.setPeople(roomVO.getPeopleMin() + "~" + roomVO.getPeopleMax() + "人");
            roomVOList.add(roomVO);
        }
    }

    /*
     * @Description:   设置包间菜系
     * @Author liguiqin
     * @Date 2023/5/30 14:16
     * @param: restaurantId
     * @param: roomVOS
     * @return void
     * @UpdateDate 2023/5/30 14:16
     **/
    private void setCuisine(RestaurantBO restaurantBO, List<IndexRoomVO> roomVOS) {
        if (restaurantBO == null) {
            return;
        }
        Integer restaurantId = restaurantBO.getId();
        List<Integer> roomIds = new ArrayList<>();
        for (IndexRoomVO roomVO : roomVOS) {
            roomIds.add(roomVO.getId());
        }
        //设置菜系名称
        Map<Integer, CuisineBO> cuisineMap = new HashMap<>();
        Map<Integer, CuisineRoomBO> cuisineRoomMap = new HashMap<>();
        List<CuisineBO> cuisineList = cuisineService.listByRestaurantId(restaurantId);
        if (CollectionUtils.isEmpty(cuisineList)) {
            return;
        }
        cuisineMap = cuisineList.stream().collect(Collectors.toMap(CuisineBO::getId, Function.identity()));
        List<Integer> cuisineIds = cuisineList.stream().map(CuisineBO::getId).collect(Collectors.toList());
        List<CuisineRoomBO> cuisineRoomBOs = cuisineRoomService.listCuisineByRoomIds(roomIds, cuisineIds);
        cuisineRoomMap = cuisineRoomBOs.stream().collect(Collectors.toMap(CuisineRoomBO::getRoomId, Function.identity()));
        for (IndexRoomVO roomVO : roomVOS) {
            CuisineRoomBO roomBO = cuisineRoomMap.get(roomVO.getId());
            if (roomBO == null) {
                continue;
            }
            CuisineBO cuisineBO = cuisineMap.get(roomBO.getCuisineId());
            roomVO.setCuisineName(cuisineBO != null ? cuisineBO.getName() : "");
        }
    }
}
